package com.nlscan.ncomgateway.core.newland.profile

import android.os.NhubManager
import com.nlscan.ble.util.NLogUtil
import com.nlscan.ncomgateway.core.newland.common.NlsVariate
import com.nlscan.ncomgateway.core.newland.common.NlsVariateHelper
import com.nlscan.ncomgateway.core.newland.entity.BroadcastOutputParams

/**
 * Nls 改名为 Nts
 * 注意：修改 Nls 值时，同时会影响 App 值，因为会改变 PreferenceManager 的值
 *
 * todo 2024-07-05 缓存值更新策略，外层重新刷新一遍
 *
 */
object NtsProfileStrategy : IProfileStrategy {

    override fun updateSettings(): HashMap<String, String?>? {
        return NlsVariateHelper.updateSettings()
    }

    // TODO: 2024-07-02 从本地还是从远程获取？值更新的问题
    override fun getOutputMode(): Int {
        //当前读取本地缓存值
        return NlsVariateHelper.getOutputMode()
    }

    override fun setOutputMode(mode: Int) {
        //[bugFix]本地缓存值也要修改
        // TODO: 2024-07-03 状态值类型要做转换
        NlsVariateHelper.setOutputMode(mode)
        val ret = NlsVariate.nhubManager?.setOutpuModeOfProfile(NlsVariate.profileName, mode)
        NLogUtil.i("setOutputMode profile: ${NlsVariate.profileName}, mode: $mode, ret: $ret")
        // TODO: 2024-07-03 修改失败要还原本地缓存值

    }

    override fun getBroadcastOutputAction(): String {
        //当前读取本地缓存值
        return NlsVariateHelper.getBroadcastOutputAction()
    }

//     fun setBroadcastOutputAction(value: String) {
//        NlsVariateHelper.setBroadcastOutputAction(value)
//        // TODO: 2024-07-04 不支持设置多参数
//        //当前先临时修改
////        getNhubManager()?.setbroadcast
//    }

    override fun getBroadcastOutputExtraResult(): String {
        //当前读取本地缓存值
        return NlsVariateHelper.getBroadcastOutputExtraResult()
    }

    override fun getBroadcastOutputParams(): BroadcastOutputParams {
        //读取本地缓存值
        // TODO: 本地sp值读取不到，需要临时外层重新赋值
        return BroadcastOutputParams(
            scannerAction = NlsVariateHelper.getBroadcastOutputAction(),
            scannerExtraBarcode = NlsVariateHelper.getBroadcastOutputExtraResult(),
            scannerExtraBarcode2 = NlsVariateHelper.getBroadcastOutputExtraResult2(),
            scannerExtraBarcodeType = NlsVariateHelper.getBroadcastOutputExtraBarcodeType(),
            scannerExtraBarcodeTypeName = NlsVariateHelper.getBroadcastOutputExtraBarcodeTypeName(),
            ocrAction = NlsVariateHelper.getOcrBroadcastAction(),
            ocrExtraResultParamName = NlsVariateHelper.getOcrBroadcastResultParamName(),
            ocrExtraConfidence = NlsVariateHelper.getOcrBroadcastExtraConfidence(),
            ocrExtraAllConfidence = NlsVariateHelper.getOcrBroadcastExtraAllConfidence(),
            ocrExtraCoordinate = NlsVariateHelper.getOcrBroadcastExtraCoordinate(),
            ocrExtraOriginalData = NlsVariateHelper.getOcrBroadcastExtraOriginalData(),
            ocrExtraImage = NlsVariateHelper.getOcrBroadcastExtraImage()
        )
    }

    override fun setBroadcastOutputParams(params: BroadcastOutputParams) {
        // TODO: 2024-07-07 exScanner未找到接口，当前作为本地值处理，在外层处理
        //修改本地缓存值
        NlsVariateHelper.setBroadcastOutputAction(params.scannerAction)
        NlsVariateHelper.setBroadcastOutputExtraResult(params.scannerExtraBarcode)
        NlsVariateHelper.setBroadcastOutputExtraResult2(params.scannerExtraBarcode2)
        NlsVariateHelper.setBroadcastOutputExtraBarcodeType(params.scannerExtraBarcodeType)
        NlsVariateHelper.setBroadcastOutputExtraBarcodeTypeName(params.scannerExtraBarcodeTypeName)

        NlsVariateHelper.setOcrBroadcastAction(params.ocrAction)
        NlsVariateHelper.setOcrBroadcastResultParamName(params.ocrExtraResultParamName)
        NlsVariateHelper.setOcrBroadcastExtraConfidence(params.ocrExtraConfidence)
        NlsVariateHelper.setOcrBroadcastExtraAllConfidence(params.ocrExtraAllConfidence)
        NlsVariateHelper.setOcrBroadcastExtraCoordinate(params.ocrExtraCoordinate)
        NlsVariateHelper.setOcrBroadcastExtraOriginalData(params.ocrExtraOriginalData)
        NlsVariateHelper.setOcrBroadcastExtraImage(params.ocrExtraImage)

        getNhubManager()?.setOutputBroadcastParamsSecOfProfile(
            getProfileName(), params.scannerAction, params.scannerExtraBarcode,
            params.scannerExtraBarcode2, params.scannerExtraBarcodeType,
            params.scannerExtraBarcodeTypeName
        )
        //OCR
        getNhubManager()?.setBroadcastSettings(getProfileName(),NhubManager.TYPE_OCR,
            params.ocrAction,params.ocrExtraResultParamName,
            NlsVariate.ocrBroadcastExtraMap)


    }

    override fun setIndicateSoundEnable(enable: Boolean): Boolean {
        //更新本地缓存
        NlsVariateHelper.setIndicateSoundEnable(enable)
        return getNhubManager()?.setIndicateSoundEnableOfProfile(getProfileName(), enable) ?: false
    }

    override fun getIndicateVibrateEnable(): Boolean {
        //当前读取本地缓存值
        return NlsVariateHelper.getIndicateVibrateEnable()
    }

    override fun setIndicateVibrateEnable(enable: Boolean): Boolean {
        //更新本地缓存
        NlsVariateHelper.setIndicateVibrateEnable(enable)
        return getNhubManager()?.setIndicateVibrateEnableOfProfile(getProfileName(), enable)
            ?: false
    }

    override fun getIndicateLedEnable(): Boolean {
        //当前读取本地缓存值
        return NlsVariateHelper.getIndicateLedEnable()
    }

    override fun setIndicateLedEnable(enable: Boolean): Boolean {
        //更新本地缓存
        NlsVariateHelper.setIndicateLedEnable(enable)
        return getNhubManager()?.setIndicateLedEnableOfProfile(getProfileName(), enable) ?: false
    }

    override fun getPrefix(): String {
        //当前读取本地缓存值
        return NlsVariateHelper.getPrefix()
    }

    override fun getPrefixEnable(): Boolean {
        //当前读取本地缓存值
        return NlsVariateHelper.getPrefixEnable()
    }

    override fun setPrefixParams(value: String, enable: Boolean): Boolean {
        //更新本地缓存
        NlsVariateHelper.setPrefix(value)
        NlsVariateHelper.setPrefixEnable(enable)
        return getNhubManager()?.setPrefixOfProfile(getProfileName(), value, enable) ?: false
    }

    override fun getSuffix(): String {
        //当前读取本地缓存值
        return NlsVariateHelper.getSuffix()
    }

    override fun getSuffixEnable(): Boolean {
        //当前读取本地缓存值
        return NlsVariateHelper.getSuffixEnable()
    }

    override fun setSuffixParams(value: String, enable: Boolean): Boolean {
        //更新本地缓存
        NlsVariateHelper.setSuffix(value)
        NlsVariateHelper.setSuffixEnable(enable)
        return getNhubManager()?.setSuffixOfProfile(getProfileName(), value, enable) ?: false
    }


    /**
     * -- 读取缓存值
     */
    private fun getNhubManager() = NlsVariate.nhubManager


    private fun getProfileName() = NlsVariate.profileName

}